package com.jinmdz.app.api.wrapper;

import com.jinmdz.app.api.base.BaseService;
import com.jinmdz.app.api.model.system.UserItem;
import com.jinmdz.app.dao.model.dictionary.DataDictionaryItem;
import com.jinmdz.fmis.common.ext.StringBuilderExt;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.constant.EDictCode;
import com.jinmdz.fmis.core.exception.ActionException;
import com.jinmdz.fmis.mapper.entity.FuneralBusinessLogEntity;
import com.jinmdz.fmis.mapper.entity.SystemOperationLogEntity;
import com.jinmdz.fmis.mapper.mapper.FuneralBusinessLogMapper;
import com.jinmdz.fmis.mapper.mapper.SystemOperationLogMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * OperationLog封装处理类
 *
 * @author LiCongLu
 * @date 2020-01-09 11:55
 */
@Component
public class OperationWrapper extends BaseService {

    @Resource
    private SystemOperationLogMapper systemOperationLogMapper;

    @Resource
    private FuneralBusinessLogMapper funeralBusinessLogMapper;

    @Resource
    private DictionaryWrapper dictionaryWrapper;

    /**
     * 操作日志记录
     *
     * @param userItem 当前号
     * @param typeCode 当前操作类型编码
     * @return
     * @author LiCongLu
     * @date 2020-01-15 10:12
     */
    public void systemLog(UserItem userItem, Integer typeCode) throws ActionException {
        SystemOperationLogEntity entity = getEntity(userItem, SystemOperationLogEntity.class);
        // 日志编码
        entity.setCreateUserName(userItem.getFullName());

        // 添加操作日志
        addOperationLog(null, typeCode, entity);
    }

    /**
     * 添加业务日志记录
     *
     * @param userItem     当前帐号
     * @param businessCode 业务编码
     * @param typeCode     业务操作编码
     * @return
     * @author LiCongLu
     * @date 2020-02-11 10:17
     */
    public FuneralBusinessLogEntity businessLog(UserItem userItem, String businessCode, Integer typeCode) throws ActionException {
        FuneralBusinessLogEntity entity = getEntity(userItem, FuneralBusinessLogEntity.class);
        // 业务编码
        entity.setBusinessCode(businessCode)
                .setBusinessTypeCode(typeCode)
                .setCreateUserName(userItem.getFullName());

        // 字典编码文本
        DataDictionaryItem dictionaryItem = dictionaryWrapper.getDataDictionaryItem(EDictCode.BUSINESS_LOG, typeCode);
        if (dictionaryItem != null) {
            entity.setBusinessType(dictionaryItem.getDataText())
                    .setBusinessDescribe(dictionaryItem.getDataText());
        }

        entity.setRemark(entity.getBusinessDescribe());

        funeralBusinessLogMapper.insertFuneralBusinessLog(entity);

        // 判断日志结果
        if (DataUtil.invalid(entity.getId())) {
            throw exception("业务日志错误");
        }

        return entity;
    }

    /**
     * 添加操作日志记录
     *
     * @param userItem          当前帐号
     * @param businessLogEntity 业务日志
     * @param typeCode          当前操作类型编码
     * @param builder           日志内容
     * @return
     * @author LiCongLu
     * @date 2020-02-11 10:04
     */
    public Integer operationLog(UserItem userItem, FuneralBusinessLogEntity businessLogEntity, Integer typeCode, StringBuilderExt builder) throws ActionException {
        // 添加操作日志
        return operationLog(userItem, businessLogEntity, typeCode
                , builder.toString(), null, null, null);
    }

    /**
     * 添加操作日志记录
     *
     * @param userItem          当前帐号
     * @param typeCode          当前操作类型编码
     * @param businessLogEntity 业务日志
     * @param logContent        日志内容
     * @param logForm           表单数据
     * @param oldValue          旧数据
     * @param newValue          新数据
     * @return
     * @author LiCongLu
     * @date 2020-02-24 17:09
     */
    public Integer operationLog(UserItem userItem, FuneralBusinessLogEntity businessLogEntity, Integer typeCode
            , String logContent, String logForm, String oldValue, String newValue) throws ActionException {
        SystemOperationLogEntity entity = getEntity(userItem, SystemOperationLogEntity.class);
        // 操作日志内容
        entity.setCreateUserName(userItem.getFullName())
                .setLogContent(logContent)
                .setOldValue(oldValue)
                .setNewValue(newValue)
                .setLogForm(logForm);

        // 添加操作日志
        return addOperationLog(businessLogEntity, typeCode, entity);
    }

    /**
     * 添加系统操作日志记录
     *
     * @param userItem   当前帐号
     * @param typeCode   当前操作类型编码
     * @param logContent 日志内容
     * @param logForm    表单数据
     * @param oldValue   旧数据
     * @param newValue   新数据
     * @return
     * @author LiCongLu
     * @date 2020-03-10 15:52
     */
    public Integer operationLog(UserItem userItem, Integer typeCode
            , String logContent, String logForm, String oldValue, String newValue) throws ActionException {
        SystemOperationLogEntity entity = getEntity(userItem, SystemOperationLogEntity.class);
        // 操作日志内容
        entity.setCreateUserName(userItem.getFullName())
                .setLogContent(logContent)
                .setOldValue(oldValue)
                .setNewValue(newValue)
                .setLogForm(logForm);

        // 添加操作日志
        return addOperationLog(null, typeCode, entity);
    }

    /**
     * 添加操作日志记录
     *
     * @param businessLogEntity 业务日志实体
     * @param typeCode          当前操作类型编码
     * @param entity            操作日志实体
     * @return
     * @author LiCongLu
     * @date 2020-02-24 16:57
     */
    private Integer addOperationLog(FuneralBusinessLogEntity businessLogEntity, Integer typeCode, SystemOperationLogEntity entity) throws ActionException {

        // 设置操作类型
        entity.setOperationTypeCode(typeCode);

        // 字典编码文本
        DataDictionaryItem dictionaryItem = dictionaryWrapper.getDataDictionaryItem(EDictCode.OPERATION_LOG, typeCode);
        if (dictionaryItem != null) {
            entity.setOperationType(dictionaryItem.getDataText())
                    .setLogDescribe(dictionaryItem.getDataText());

            if (DataUtil.invalid(entity.getLogContent())) {
                entity.setLogContent(dictionaryItem.getDataText());
            }
        } else {
            // 设置空类型
            entity.setOperationType("无");
        }

        // 避免空日志内容
        if (DataUtil.invalid(entity.getLogContent())) {
            entity.setLogContent("无");
        }

        // 更新业务日志
        if (businessLogEntity != null) {
            entity.setBusinessCode(businessLogEntity.getBusinessCode())
                    .setBusinessTypeCode(businessLogEntity.getBusinessTypeCode())
                    .setBusinessType(businessLogEntity.getBusinessType())
                    .setBusinessDescribe(businessLogEntity.getBusinessDescribe());
        }

        // 插入操作日志
        systemOperationLogMapper.insertSystemOperationLog(entity);

        // 判断日志结果
        if (DataUtil.invalid(entity.getId())) {
            throw exception("操作日志错误");
        }

        return entity.getId();
    }
}
